package com.volvo.caretrack.mobile.services;

import java.util.concurrent.atomic.AtomicReference;

import com.volvo.caretrack.mobile.exception.AuthException;
import com.volvo.caretrack.mobile.feed.FeedDeskFactory;
import com.volvo.caretrack.mobile.feed.IAuthenticationDesk;
import com.volvo.caretrack.mobile.model.auth.Session;

public class AuthenticationService {

private static AtomicReference<AuthenticationService> _instance = new AtomicReference<AuthenticationService>();
	
	IAuthenticationDesk authenticationDesk;
	
	private AuthenticationService() {
		initialize();
	}
	
	private void initialize() {
		authenticationDesk = FeedDeskFactory.getAuthenticationDesk();
	}
	
	public static AuthenticationService getInstance()
    {
		AuthenticationService m = _instance.get();
        if (null == m)
        {
            _instance.compareAndSet(null, new AuthenticationService());
            m = _instance.get();
        }
        return m;
    }
	
	public Session login(String user, String password) throws AuthException {
		return authenticationDesk.login(user, password);
	}
	
	public void logout(String sessionID) throws AuthException {
		Session session = new Session(sessionID);
		authenticationDesk.logout(session);
	}
	
	public boolean isAlive(){
		return true;
	}
}
